﻿using Anke.SHManage.BLL;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.IM.Controllers
{
    public class I_InvoiceManageController : Controller
    {
        //
        // GET: /IM/I_InvoiceManage/

        #region 重打发票审核
        
      
        public ActionResult InvoiceManageList()
        {
            return View();
        }

        public ActionResult DataLoad()
        {
            //统计时间
            DateTime StartDate = Convert.ToDateTime(Request.Form["StartDate"]); //查询起时间 单一参数
            DateTime EndDate = Convert.ToDateTime(Request.Form["EndDate"]); //查询止时间 单一参数
         
            string IFCheck = Request.Form["IFCheck"].ToString();

            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求的页码
            int pageIndex = int.Parse(Request.Form["page"]);
            int rowCount = 0;

            //IFCheck 的3 种情况
            switch (IFCheck) {
                case "":
                    {
                        var list = new M_PatientChargeLogBLL().GetPagedList(pageIndex, pageSize, ref rowCount, p => p.Date >= StartDate && p.Date < EndDate
                   && p.Isvalid == false, p => p.Date).Select(p => p.ToPOCO());
                        // 生成规定格式的 json字符串发回 给异步对象
                        Model.EasyUIModel.DataGridModel dgModel = new Model.EasyUIModel.DataGridModel()
                        {
                            total = rowCount,
                            rows = list,
                            footer = null
                        };
                        return Json(dgModel);
                    }
                case "true":
                    {
                        var list = new M_PatientChargeLogBLL().GetPagedList(pageIndex, pageSize, ref rowCount, p => p.Date >= StartDate && p.Date < EndDate && p.IFCheck == true
                  && p.Isvalid == false, p => p.Date).Select(p => p.ToPOCO());
                        // 生成规定格式的 json字符串发回 给异步对象
                        Model.EasyUIModel.DataGridModel dgModel = new Model.EasyUIModel.DataGridModel()
                        {
                            total = rowCount,
                            rows = list,
                            footer = null
                        };
                        return Json(dgModel);
                    }
                case "false":
                    {
                        var list = new M_PatientChargeLogBLL().GetPagedList(pageIndex, pageSize, ref rowCount, p => p.Date >= StartDate && p.Date < EndDate && p.IFCheck != true
            && p.Isvalid == false, p => p.Date).Select(p => p.ToPOCO());
                        // 生成规定格式的 json字符串发回 给异步对象
                        Model.EasyUIModel.DataGridModel dgModel = new Model.EasyUIModel.DataGridModel()
                        {
                            total = rowCount,
                            rows = list,
                            footer = null
                        };
                        return Json(dgModel);
                    }
                default:
                    return Json("");
            }
        }



        //2016-4-11 发票号审核
        [HttpPost]
        public ActionResult SavaPickCheck()
        {

            string sDeliveryDetailInfo = Request.Form["sDeliveryDetailInfo"].ToString();
            List<M_PatientChargeLog> relist = JsonHelper.GetJsonInfoBy<List<M_PatientChargeLog>>(sDeliveryDetailInfo); //前台传来的从表数据
         
            if (relist.Count > 0)
            {
                DateTime tempNow = DateTime.Now;
                int resulDeliveryDetail = relist.Count;
             

                //循环将入库字表的IFcheck和CheckTime值改变
                for (int i = 0; i < relist.Count; i++)
                {
                  var  tempID = new Guid(); 
                    
                     tempID = relist[i].ID;

                    //先根据EntryDetailCode 将实体取出来，看是否存在IFCHECK 和CheckTime 如果存在就不更改
                    M_PatientChargeLog info = new M_PatientChargeLogBLL().GetModelWithOutTrace(p => p.ID == tempID);

                    if (info.IFCheck != true || info.IFCheck == null) //判断是否有I_EntryDetail 
                    {
                        info.IFCheck = true;
                        info.CheckTime = tempNow;


                        int resultDetail = new M_PatientChargeLogBLL().Modify(info, "IFCheck", "CheckTime");
                        resulDeliveryDetail = resulDeliveryDetail + resultDetail;
                    }
                    else
                    {
                       
                    }
                 
                }
                if (resulDeliveryDetail >= relist.Count)
                {
                    return this.JsonResult(Utility.E_JsonResult.OK, "审核信息记录成功！", null, null);
                }
                else
                {
                    return this.JsonResult(Utility.E_JsonResult.Error, "审核信息记录失败！", null, null);
                }
            }
            else
            {
                return Json("");
            }

        }
        #endregion


        #region 应收实收不等审核

        public ActionResult InvoiceManageList2()
        {
            return View();
        }
        public ActionResult DataLoad2()
        {
            //统计时间
            DateTime StartDate = Convert.ToDateTime(Request.Form["StartDate"]); //查询起时间 单一参数
            DateTime EndDate = Convert.ToDateTime(Request.Form["EndDate"]); //查询止时间 单一参数

            string IFCheck = Request.Form["IFCheck"].ToString();

            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求的页码
            int pageIndex = int.Parse(Request.Form["page"]);
            int rowCount = 0;

            //IFCheck 的3 种情况
            switch (IFCheck)
            {
                case "":
                    {
                        var list = new M_PatientChargeBLL().GetPagedList(pageIndex, pageSize, ref rowCount, p => p.Date >= StartDate && p.Date < EndDate
                   && p.PaidMoney!= p.ReceivableTotal, p => p.Date).Select(p => p.ToPOCO());
                        // 生成规定格式的 json字符串发回 给异步对象
                        Model.EasyUIModel.DataGridModel dgModel = new Model.EasyUIModel.DataGridModel()
                        {
                            total = rowCount,
                            rows = list,
                            footer = null
                        };
                        return Json(dgModel);
                    }
                case "true":
                    {
                        var list = new M_PatientChargeBLL().GetPagedList(pageIndex, pageSize, ref rowCount, p => p.Date >= StartDate && p.Date < EndDate && p.IFCheck == true
                  && p.PaidMoney != p.ReceivableTotal, p => p.Date).Select(p => p.ToPOCO());
                        // 生成规定格式的 json字符串发回 给异步对象
                        Model.EasyUIModel.DataGridModel dgModel = new Model.EasyUIModel.DataGridModel()
                        {
                            total = rowCount,
                            rows = list,
                            footer = null
                        };
                        return Json(dgModel);
                    }
                case "false":
                    {
                        var list = new M_PatientChargeBLL().GetPagedList(pageIndex, pageSize, ref rowCount, p => p.Date >= StartDate && p.Date < EndDate && p.IFCheck != true
            && p.PaidMoney != p.ReceivableTotal, p => p.Date).Select(p => p.ToPOCO());
                        // 生成规定格式的 json字符串发回 给异步对象
                        Model.EasyUIModel.DataGridModel dgModel = new Model.EasyUIModel.DataGridModel()
                        {
                            total = rowCount,
                            rows = list,
                            footer = null
                        };
                        return Json(dgModel);
                    }
                default:
                    return Json("");
            }
        }



        //2016-4-11 发票号审核
        [HttpPost]
        public ActionResult SavaPickCheck2()
        {

            string sDeliveryDetailInfo = Request.Form["sDeliveryDetailInfo"].ToString();
            List<M_PatientCharge> relist = JsonHelper.GetJsonInfoBy<List<M_PatientCharge>>(sDeliveryDetailInfo); //前台传来的从表数据

            if (relist.Count > 0)
            {
                DateTime tempNow = DateTime.Now;
                int resulDeliveryDetail = relist.Count;


                //循环将入库字表的IFcheck和CheckTime值改变
                for (int i = 0; i < relist.Count; i++)
                {


                    string tempTaskCode = relist[i].TaskCode;

                    //先根据EntryDetailCode 将实体取出来，看是否存在IFCHECK 和CheckTime 如果存在就不更改
                    M_PatientCharge info = new M_PatientChargeBLL().GetModelWithOutTrace(p => p.TaskCode == tempTaskCode);

                    if (info.IFCheck != true || info.IFCheck == null) //判断是否有I_EntryDetail 
                    {
                        info.IFCheck = true;
                        info.CheckTime = tempNow;


                        int resultDetail = new M_PatientChargeBLL().Modify(info, "IFCheck", "CheckTime");
                        resulDeliveryDetail = resulDeliveryDetail + resultDetail;
                    }
                    else
                    {

                    }

                }
                if (resulDeliveryDetail >= relist.Count)
                {
                    return this.JsonResult(Utility.E_JsonResult.OK, "审核信息记录成功！", null, null);
                }
                else
                {
                    return this.JsonResult(Utility.E_JsonResult.Error, "审核信息记录失败！", null, null);
                }
            }
            else
            {
                return Json("");
            }

        }





        #endregion

    }
}
